package org.dong.Dao;

import org.dong.Jdbc.JdbcUtil;

import java.lang.reflect.Field;
import java.math.BigDecimal;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class BeasDao {


    //增删改
    public int update(String sql, List<Object> list) throws SQLException {

        Connection conn = JdbcUtil.getConnection();
        PreparedStatement preparedStatement = null;
        assert conn != null;
        preparedStatement = conn.prepareStatement(sql);
//        System.out.println(list.size());
        for (int i = 0; i < list.size(); i++) {
            preparedStatement.setObject(i+1,list.get(i));
        }

        int rs = preparedStatement.executeUpdate();
        //关闭链接
        JdbcUtil.closeDb(null,conn,preparedStatement);

        return rs;
    }

    //查询
    public List<Object> select(String sql,List<Object> list,Class<?> cls) throws SQLException, InstantiationException, IllegalAccessException {
        Connection conn = JdbcUtil.getConnection();
        PreparedStatement preparedStatement = null;
        preparedStatement = conn.prepareStatement(sql);

        for (int i = 0; i < list.size(); i++) {
            preparedStatement.setObject(i+1,list.get(i));
        }


        ResultSet rs = preparedStatement.executeQuery();
        //动态加载类
        List<Object> data = new ArrayList<>();
        while(rs.next()){

            Object obj = cls.newInstance();
            Field[] fields = cls.getDeclaredFields();
            for (Field field : fields) {

                field.setAccessible(true);
                field.getDeclaredAnnotations();

                if(field.getType() == BigDecimal.class){
                    field.set(obj,rs.getBigDecimal(field.getName()));
                }else if(field.getType() == String.class){
                    field.set(obj,rs.getString(field.getName()));
                }else if(field.getType() == int.class){
                    field.set(obj,rs.getInt(field.getName()));
                }

            }
            data.add(obj);
        }

        //关闭数据库链接
        JdbcUtil.closeDb(rs,conn,preparedStatement);

        return data;
    }
    public <T> T selectOne(String sql,List<Object> list,Class<T> cls) throws SQLException {
        Connection conn = JdbcUtil.getConnection();
        PreparedStatement preparedStatement = null;
        assert conn != null;
        preparedStatement = conn.prepareStatement(sql);

        for (int i = 0; i < list.size(); i++) {

            preparedStatement.setObject(i+1,list.get(i));
        }

        ResultSet rs = preparedStatement.executeQuery();
        if(rs.next()){

            if(cls == BigDecimal.class){
                BigDecimal bigDecimal = rs.getBigDecimal(1);
                //关闭数据库链接
                JdbcUtil.closeDb(rs,conn,preparedStatement);
                return cls.cast(bigDecimal);
            }else if(cls == String.class){
                String string = rs.getString(1);
                //关闭数据库链接
                JdbcUtil.closeDb(rs,conn,preparedStatement);
                return cls.cast(string);
            }else if(cls == Integer.class){
                Integer anInt = rs.getInt(1);
                //关闭数据库链接
                JdbcUtil.closeDb(rs,conn,preparedStatement);
                return cls.cast(anInt);
            }




        }

        //关闭数据库链接
        JdbcUtil.closeDb(rs,conn,preparedStatement);
        return null;

    }


}
